// pages/myCart/myCart.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //selected: false, //列表的选项
        startX: 0,
        startY: 0, //开始的坐标
        selectAllStatus: false, //全选
        selectButton: false, //结算
        totalPrice: '0.00', //总价格
        num: 0, //选中了几个商品
        list: [], //购物车数据
    },
    // 1.手指触摸动作开始
    touchstart: function (params) {
        //console.log(params);
        //保存手指触摸时的坐标
        this.setData({
            startX: params.changedTouches[0].clientX,
            startY: params.changedTouches[0].clientY
        });
    },
    // 2.手指触摸后移动
    touchmove: function (params) {
        //console.log(params);
        //获取手指触摸时的坐标
        var index = params.currentTarget.dataset.index;
        var startX = this.data.startX;
        var startY = this.data.startY;
        //获取手指触摸后的坐标
        var moveX = params.changedTouches[0].clientX;
        var moveY = params.changedTouches[0].clientY;
        //判断左滑还是右滑
        var list = this.data.list;
        //清空所有左滑
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            element.isTouchMove = false;
        }
        if (moveX < startX - 30) {
            list[index].isTouchMove = true;
        } else {
            list[index].isTouchMove = false;
        }
        //同步数据
        this.setData({
            list: list
        });

    },
    // 3.获取购物车数据
    getShop: function () {
        wx.request({
          url: 'http://iwenwiki.com:3002/api/cart/list',
          success: (params) => {
              //console.log(params);
              if (params.data.status == 200) {
                  var list = params.data.data.result;
                  var num = 0;
                  //在获取购物车数据之前的选中的商品
                  var selectAllStatus = this.data.selectAllStatus;
                  if (selectAllStatus) {
                      for (let i = 0; i < list.length; i++) {
                          const element = list[i];
                          element.selected = true;
                      }
                      var num = list.length;
                  }
                  this.setData({
                      list: list,
                      num: num
                  })
                  this.goTotalPrice();
              };
          }
        })
    },
    // 4.删除购物车中的数据
    remove: function (params) {
      wx.request({
        url: 'http://iwenwiki.com:3002/api/cart/delete',
        data: {
            id: params.currentTarget.dataset.id
        },
        success: (params) => {
            console.log(params);
            if (params.data.status == 200) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'none'
                });
                this.getShop(); //重新请求数据
            }
        }
      })  
    },
    // 5.计算总价格
    goTotalPrice: function (params) {
        var list = this.data.list;
        var total = 0; //总价格
        for (let i = 0; i < list.length; i++) {
            const item = list[i];
            if (item.selected) {
                total += list[i].price * list[i].num
            }
        }
        this.setData({
            totalPrice: Math.floor(total * 100) / 100 //向下取整保留2位小数
        })
    },
    // 6.点击选择购物车物品
    selectedList: function (params) {
        var index = params.currentTarget.dataset.index;
        //console.log(index)
        var list = this.data.list;
        var num = this.data.num;
        //获取原来的状态
        var selected = list[index].selected;
        list[index].selected = !selected;
        if (list[index].selected) {
            num++;
        } else {
            num--;
        }
        this.setData({
            list: list,
            num: num
        })
        //结算
        if (num > 0) {
            this.setData({
                selectButton: true
            })
        } else {
            this.setData({
                selectButton: false
            })
        }
        //全选
        if (num == list.length) {
            this.setData({
                selectAllStatus: true
            })
        } else {
            this.setData({
                selectAllStatus: false
            })
        }
        this.goTotalPrice();
    },
    // 7.全选
    selectedAll: function (params) {
        //selectAllStatus控制是否全选
        var selectAllStatus = !this.data.selectAllStatus;
        var list = this.data.list;
        var num = this.data.num;
        var selectButton = this.data.selectButton;
        for (let i = 0; i < list.length; i++) { //全部商品选中或不选中
            const element = list[i];
            element.selected = selectAllStatus;
        }
        //改变选中的数量
        if (selectAllStatus) {
            num = list.length;
            selectButton = true; //结算按钮高亮
        } else {
            num = 0;
            selectButton = false;
        }
        this.setData({
            list: list,
            selectAllStatus: selectAllStatus,
            num: num,
            selectButton: selectButton
        })
        this.goTotalPrice();
    },
    // 8.点击+按钮增加商品数量 
    addShop: function (params) {
        var index = params.currentTarget.dataset.index;
        var num = params.currentTarget.dataset.num;
        var list = this.data.list;
        num++;
        list[index].num = num;
        this.setData({
            list: list
        })
        wx.request({
          url: 'http://iwenwiki.com:3002/api/cart/update',
          data: {
              id: params.currentTarget.dataset.id,
              num: num
          },
          success: (params) => {
              console.log(params)
              if (params.data.status == 200) {
                  //添加成功
              }
          }
        })
        this.goTotalPrice();
    },
    // 9.点击-按钮减少商品数量
    reduce: function (params) {
        var index = params.currentTarget.dataset.index;
        var num = params.currentTarget.dataset.num;
        var list = this.data.list;
        if (num > 1) {
            num--;
        }
        list[index].num = num;
        this.setData({
            list: list
        })
        wx.request({
          url: 'http://iwenwiki.com:3002/api/cart/update',
          data: {
              id: params.currentTarget.dataset.id,
              num: num
          },
          success: (params) => {
              console.log(params)
              if (params.data.status == 200) {
                  //添加成功
              }
          }
        })
        this.goTotalPrice();
    },
    // 10.支付
    balance: function (params) {
        //用户信息 传递总价值 总的其他信息
        wx.redirectTo({
          url: '../complete/complete',
        })
        // wx.navigateTo({
        //   url: '../complete/complete',
        // })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getShop(); //获取购物车数据
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.setData({//切换页面后取消全选
            selectAllStatus: false, //全选
            selectButton: false, //结算
            totalPrice: '0.00', //总价格
            num: 0, //选中了几个商品
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})